package com.qf.service.impl;

import com.github.pagehelper.PageInfo;
import com.qf.dao.TbCourceDao;
import com.qf.dao.TbStudentDao;
import com.qf.dao.TbVariableDao;
import com.qf.dto.CourceDto;
import com.qf.pojo.*;
import com.qf.service.CourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * created by 赵文韬 on 2022/5/27 14:41
 */
@Service
public class CourceServiceImpl implements CourceService {
    @Resource
    private TbCourceDao tbCourceDao;
    @Resource
    private TbVariableDao tbVariableDao;
    @Resource
    private TbStudentDao tbStudentDao;
    @Override
    public List<TbCource> selectAllCource() {
        //查询所有课程信息
        List<TbCource> tbCourceList = tbCourceDao.selectByExample(null);

        return tbCourceList;
    }
    //根据课程名分页查询
    @Override
    public PageInfo<TbCource> findByPage(int pageNum, int pageSize, String courseName) {
        TbCourceExample tbCourceExample = new TbCourceExample();
        TbCourceExample.Criteria criteria = tbCourceExample.createCriteria();
        criteria.andNameLike("%"+courseName+"%");
        /*criteria.andNameEqualTo(courseName);*/
        List<TbCource> tbCourceList = tbCourceDao.selectByExample(tbCourceExample);
        PageInfo<TbCource> pageInfo = new PageInfo<>( tbCourceList);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(tbCourceList.size());
        return pageInfo;
    }

    @Override
    public int updateCource(CourceDto courceDto) {
        TbCource tbCource = new TbCource();
        tbCource.setId(courceDto.getId());
        tbCource.setCapacity(courceDto.getCapacity());
        tbCource.setCount(courceDto.getCount());
        tbCource.setName(courceDto.getName());
        tbCource.setTeachername(courceDto.getTeachername());
        int i = tbCourceDao.updateByPrimaryKeySelective(tbCource);
        return i;
    }

    @Override
    public int insertCource(CourceDto courceDto) {
        TbCource tbCource = new TbCource();
        tbCource.setId(courceDto.getId());
        tbCource.setCapacity(courceDto.getCapacity());
        tbCource.setName(courceDto.getName());
        tbCource.setTeachername(courceDto.getTeachername());
        int i = tbCourceDao.insertSelective(tbCource);
        return i;
    }

    @Override
    public int deleteCource(int id) {
        int i = tbCourceDao.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public List<TbStudent> selectById(int id) {
        TbVariableExample variableExample = new TbVariableExample();
        TbVariableExample.Criteria criteria = variableExample.createCriteria();
        criteria.andCidEqualTo(id);
        //先根据课程id找到sid集合
        List<TbVariable> tbVariables = tbVariableDao.selectByExample(variableExample);
        ArrayList<Object> sids = new ArrayList<>();
        for (int i = 0; i < tbVariables.size(); i++) {
            Integer sid = tbVariables.get(i).getSid();
            sids.add(sid);
        }
        ArrayList students = new ArrayList<>();
        for (int i = 0; i < sids.size(); i++) {
            TbStudent tbStudent = tbStudentDao.selectByPrimaryKey((Integer) sids.get(i));
            students.add(tbStudent);
        }
        return students;
    }

}
